package com.ducanh.doan.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteHelper extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "database.db";
	private static final int DATABASE_VERSION = 1;
	
	public static final String TABLE_USER = "user";
	public static final String TABLE_ACCOUNT = "account";
	
	// table user 
	public static final String KEY_USER_ID = "id";
	public static final String KEY_USER_NAME = "name";
	public static final String KEY_USER_ORDER = "order_no";
	public static final String KEY_USER_DESCRIPTION = "desciption";
	
	public static final String KEY_ACCOUNT_ID = "id";
	public static final String KEY_ACCOUNT_USER_ID = "user_id";
	public static final String KEY_ACCOUNT_NAME = "name";
	public static final String KEY_ACCOUNT_DESCRIPTION = "description";
	public static final String KEY_ACCOUNT_ORDER = "order_no";
	public static final String KEY_ACCOUNT_STATUS = "status";
	public static final String KEY_ACCOUNT_EXPIRATION = "expiration";
	public static final String KEY_ACCOUNT_ISDEFAULT = "is_default";
	public static final String KEY_ACCOUNT_CURRENCY = "currency";
	public static final String KEY_ACCOUNT_SECURITY_CODE = "security_code";
	public static final String KEY_ACCOUNT_IS_CREDIT_CARD = "is_credit_card";
	public static final String KEY_ACCOUNT_IMAGE = "image";
	public static final String KEY_ACCOUNT_BALANCE = "balance";
	
	
	public static final String SQL_CREATE_TABLE_USER = String.format("CREATE TABLE %s(%s INTEGER PRIMARY KEY AUTOINCREMENT, " +
			"%s VARCHAR(50), %s INTEGER, %s TEXT)", TABLE_USER, KEY_USER_ID, KEY_USER_NAME, KEY_USER_ORDER, KEY_USER_DESCRIPTION);
	
	public static final String SQL_CREATE_TABLE_ACCOUNT = String.format("CREATE TABLE %s(%s INTEGER PRIMARY KEY AUTOINCREMENT, " +
			"%s INTEGER, %s VARCHAR(50), %s TEXT, %s INTEGER, %s INTEGER, %s VARCHAR, %s INTEGER, %s VARCHAR, %s VARCHAR, %s INTEGER DEFAULT (0), %s VARCHAR, %s NUMERIC)", TABLE_ACCOUNT, 
			KEY_ACCOUNT_ID, KEY_ACCOUNT_USER_ID, KEY_ACCOUNT_NAME, KEY_ACCOUNT_DESCRIPTION, KEY_ACCOUNT_ORDER, KEY_ACCOUNT_STATUS, KEY_ACCOUNT_EXPIRATION, KEY_ACCOUNT_ISDEFAULT, KEY_ACCOUNT_CURRENCY, KEY_ACCOUNT_SECURITY_CODE,
			KEY_ACCOUNT_IS_CREDIT_CARD, KEY_ACCOUNT_IMAGE, KEY_ACCOUNT_BALANCE);
	
	public SQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_TABLE_USER);
		db.execSQL(SQL_CREATE_TABLE_ACCOUNT);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE " + TABLE_USER);
		db.execSQL("DROP TABLE " + TABLE_ACCOUNT);
		onCreate(db);
	}
}
